package com.wn.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wn.common.PageParam;
import com.wn.mapper.CostMapper;
import com.wn.mapper.CostOtherMapper;
import com.wn.mapper.PatientDetailMapper;
import com.wn.mapper.UserMapper;
import com.wn.ov.PatientOv;
import com.wn.pojo.Cost;
import com.wn.pojo.CostOther;
import com.wn.pojo.PatientDetail;
import com.wn.pojo.User;
import com.wn.resopnse.ResponseCode;
import com.wn.resopnse.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 病人的详情表
 */
@Service
public class PatientService {

    @Autowired
    private PatientDetailMapper patientDetailMapper;

    @Autowired
    private CostOtherMapper costOtherMapper;

    @Autowired
    private CostMapper costMapper;//药品的价格


    @Autowired
    private UserMapper userMapper;
    //添加其他费用的住院天数和对应的金额
    public ServerResponse insertCostOther(Integer patientId,Integer id){

        //通过病人的id来查询他的住院天数
        PatientDetail patientDetail = patientDetailMapper.selectByPatientId(patientId);

        //获取开始时间和结束住院时间
        Date start = patientDetail.getStartTime();
        Date end = patientDetail.getEndTime();

        //天数
        int day = (int)(end.getTime()-start.getTime())/(24*60*60*1000);

        //通过病人的id来查找cost
        Cost cost = costMapper.selectByPatientId(patientId);

        //添加costOther的数据
        CostOther costOther = new CostOther();
        costOther.setName("住院");
        costOther.setNumber(day);
        costOther.setPrice(new BigDecimal(100));
        costOther.setCostId(cost.getId());
        costOther.setDoctorId(id);
        //添加
        int i = costOtherMapper.insertSelective(costOther);
        if(i !=0){
            return new ServerResponse(ResponseCode.SUCCESS,"添加成功");
        }else{
            return new ServerResponse(ResponseCode.ERROR,"添加失败");
        }
    }

    //获取病人详情
    public PageInfo<PatientOv> getPatientList(PageParam<PatientOv> pageParam) {
        if(!StringUtils.isEmpty(pageParam.getSuperSearchKeyWord())){
            //执行高级检索
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

            List<PatientOv> ovList=patientDetailMapper.superSearch(pageParam.getSuperSearchKeyWord());
            for(PatientOv patientOv : ovList){
                if (patientOv.getStatus()==1){
                    patientOv.setStatu("住院");
                }else if(patientOv.getStatus()==0){
                    patientOv.setStatu("未住院");
                }
            }

            PageInfo<PatientOv> ovPageInfo = new PageInfo<PatientOv>(ovList);
            return ovPageInfo;

        }else{
            //分页
            PageHelper.startPage(pageParam.getPageNum(), pageParam.getPageSize());

            List<PatientOv> ovList = patientDetailMapper.getPatientList();
            for (PatientOv patientOv : ovList) {
                if (patientOv.getStatus()==1){
                    patientOv.setStatu("住院");
                }else if(patientOv.getStatus()==0){
                    patientOv.setStatu("未住院");
                }
            }

            PageInfo<PatientOv> ovPageInfo = new PageInfo<PatientOv>(ovList);
            return ovPageInfo;
        }
    }

    public ServerResponse deletePatientDetail(PatientDetail patientDetail){
       //通过病人的id来找病人详情的id
        PatientDetail patientDetail1 = patientDetailMapper.selectByPatientId(patientDetail.getId());
        int i = patientDetailMapper.deleteByPrimaryKey(patientDetail1.getId());
        if(i !=0){
            return new ServerResponse(ResponseCode.SUCCESS,"成功");
        }else{
            return new ServerResponse(ResponseCode.ERROR,"失败");
        }
    }

}
